
const configJs = require("./config.js");

// 请求头配置：传参以json对象方式
const header = {'content-type': 'application/json'};

/**
 * request请求
 * @param url 		请求地址，以/开始
 * @param method 	请求方式
 * @param data 		请求参数json对象      
 */
export function request(url = '', method = 'POST', data = {}){
	if (url.slice(0, 1) !== '/') url = `/${url}`;
	
    return new Promise((resolve, reject) => {
        uni.request({
            method: 'post',
            url: configJs.host + url,
            data: data,
            header: header,
            dataType: 'json'
        }).then((response) => {
            let [error, res] = response;
            // 登录过期
            if(res.code == 401){
                uni.showToast({ title:'登录过期,请重新登录', duration: 2000 });
            };            
            resolve(res.data);
        }).catch((error) => {
            let [err, res] = error;
            reject(err);
        });
    });
}

/**
 * 发送post请求
 * @param url 	请求地址，以/开始
 * @param data 	请求发送的json对象    
 */
export function post(url = '', data = {}){

	if (url.slice(0, 1) !== '/') url = `/${url}`;
	
    return new Promise((resolve, reject) => {
        uni.request({
            method: 'post',
            url: dataconfigJs + url,
            data: data,
            header: header,
            dataType: 'json'
        }).then((response) => {
            let [error, res] = response;
            // 登录过期
            if(res.code == 401){
                uni.showToast({ title:'登录过期,请重新登录', duration: 2000 });
            };            
            resolve(res.data);
        }).catch((error) => {
            let [err, res] = error;
            reject(err);
        });
    });
}